import request from '@/util/util.request'
import nav from '@/util/util.nav.js'
import login from '@/util/util.login.js'
import jsAlert from '@/util/util.alert.js'
import {EngineV2} from '@/util/EngineV2'
import EZVo from '@/util/EZVo'

let pay = {
	/**
	 * @description 微信H5调起微信支付
	 * @param {object} wxJsApiParam 微信支付验证参数(后台接口调用微信商家支付平台回调)
	 * @param {string} orderCodePay 订单编号
	 */
	payWeixinH5: function(wxJsApiParam, orderCodePay) {
		console.info(wxJsApiParam);
		//内部函数
		function jsApiCall(jsStr, orderCodePay){
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest', {
					"appId": jsStr.appId,
					"nonceStr": jsStr.nonceStr,
					"package": jsStr.package,
					"paySign": jsStr.paySign,
					"signType": jsStr.signType,
					"timeStamp": jsStr.timeStamp
				},
				function(res) {
					if (res.err_msg == "get_brand_wcpay_request:ok") {
						//微信回调支付成功后，调用后台接口修改订单状态
						request('/api/xxx/xxx','post',{
							orderCodePay: orderCodePay
						}).then(res=>{
							//支付成功跳转页面
							// nav.navigateTo("/my/order")
							//支付成功返回上一页
							nav.navigateBack(1)				
						})
					} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
						alert("已取消支付")
					} else {
						alert("支付失败,请重试")
						console.info(res)
					}
				}
			)
		}
		//方法内代码 --- 开始
		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall)
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall)
			}
		} else {
			jsApiCall(wxJsApiParam, orderCodePay)
			return false
		}
	},
	/**
	 * @description 微信小程序支付
	 * @param {string} orderCodePay 订单编号 
	 * @param {string} orderPrice 订单金额 
	 */
	payWeiXinApp: function(orderCodePay,orderPrice, success, fail) {
		let mini = uni.getAccountInfoSync()
		let appid = mini.miniProgram.appId;
		//根据运行环境获取支付的提供程序
		uni.getProvider({
			service: "payment",
			success: (res) => {
				//获取到提供程序
				var provider = res.provider
				// 用户未登录
				if(login.getValue('openID') =='') {
					login.weixinInfo()
				}
				//利用openID和订单编号请求后台下单接口
				//后台接口调用微信接口回调：
				//provider:服务提供商
				//orderInfo:订单数据
				//timeStamp:时间戳从1970年1月1日至今的秒数，即当前的时间
				//nonceStr:随机字符串，长度为32个字符以下
				//package:统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=xx
				//signType:签名算法，暂支持 MD5
				//paySign: 签名 参考网址：https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3
				let openID = login.getValue('openID')
				
				var ivo =new Ivo("orderPayAtMiniapp");
				ivo.set("open_id",openID);
				ivo.set("fee",orderPrice);
				ivo.set("order_id",orderCodePay);
				ivo.set("app_id",appid);
				
				EngineV2.action(ivo, ovo => {
					// success()
					let pay = ovo.get("pay_result")
					let row = new Row()
					
					let package1 =pay.get("package");
					// let prepay_id =pay.get('prepay_id')
					let signtype =pay.get('signtype');
					let paysign =pay.get("paysign")
					let noncestr =pay.get("noncestr")
					let timestamp = pay.get('timestamp')
					// console.log("支付接口返回的ovo:",ovo,"prepay_id",prepay_id,"package1",package1,"paysign",paysign)
					// var order = JSON.parse(res.data.orderInfo)
					uni.requestPayment({
						provider: provider,
						// orderInfo: res.data.orderInfo,
						timeStamp: timestamp,
						nonceStr: noncestr,
						package: package1,
						signType: signtype,
						paySign: paysign,
						success: (res) => {
							success()
							// //微信小程序支付成功回调，调用后台接口通知后台支付成功修改数据库订单支付状态
							// request('/api/xxx/weixinPaySuccess','post',{
							// 	orderCodePay: orderCodePay
							// }).then(res=>{
							// 	// 支付成功后进行的操作
							// 	success()
							// })
						},
						fail: (res) => {
							// 支付失败
							fail()
						}
					})
				}, false)

			}
		})
	}
}

export default pay;
